﻿using System;

namespace Slc.Core.Entity
{
    public class OQLOrder
    {
        private string orderString = string.Empty;
        private OQL CurrentOQL;

        /// <summary>
        /// 获取排序字符串，OQL内部使用
        /// </summary>
        public string OrderByString
        {
            get { return orderString; }
        }

        public OQLOrder(OQL oql)
        {
            this.CurrentOQL = oql;
        }

        /// <summary>
        /// 以一个实体类对象初始化构造函数
        /// </summary>
        /// <param name="entity"></param>
        [Obsolete("构造函数已经过时，请使用其它构造函数")]
        public OQLOrder(EntityBase entity)
        {

        }
        /// <summary>
        /// 以一个或者多个实体类，来构造排序条件类。在OQL多实体类关联查询中需要使用该方法。
        /// </summary>
        /// <param name="e"></param>
        /// <param name="joinedEntitys"></param>
        [Obsolete("构造函数已经过时，请使用其它构造函数")]
        public OQLOrder(EntityBase e, params EntityBase[] joinedEntitys)
        {

        }

        private OQLOrder OrderBy(object field, string orderType)
        {
            if (CurrentOQL == null)
                throw new InvalidOperationException("当前对象实例化的时候需要使用关联的OQL实例，但使用了过期的构造函数。");
            orderString += "," + CurrentOQL.TakeStackFields() + " " + orderType;
            CurrentOQL.fieldStack.Clear();
            return this;
        }

        /// <summary>
        /// 默认排序（ASC）
        /// </summary>
        /// <param name="field">要排序的实体属性</param>
        /// <returns></returns>
        public OQLOrder OrderBy(object field)
        {
            return OrderBy(field, "ASC");
        }
        /// <summary>
        /// 升序排序ASC
        /// </summary>
        /// <param name="field">要排序的实体属性</param>
        /// <returns></returns>
        public OQLOrder Asc(object field)
        {
            return OrderBy(field, "ASC");
        }
        /// <summary>
        /// 倒序排序DESC
        /// </summary>
        /// <param name="field">要排序的实体属性</param>
        /// <returns></returns>
        public OQLOrder Desc(object field)
        {
            return OrderBy(field, "DESC");
        }
        /// <summary>
        /// 重置排序状态
        /// </summary>
        public void ReSet()
        {
            //currPropName = string.Empty;
            orderString = string.Empty;
        }
        /// <summary>
        /// 获取排序信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return orderString.Substring(1);
        }

    }
}
